package com.xjy.ftpmanagment.service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.xjy.common.service.BaseService;
import com.xjy.common.util.DateUtil;
import com.xjy.common.util.EncryptionUtil;
import com.xjy.common.util.FileUtil;
import com.xjy.common.util.PageBean;
import com.xjy.ftpmanagment.dao.FtpUserDao;
import com.xjy.ftpmanagment.dao.SysParamDao;
import com.xjy.ftpmanagment.dao.SysUserDao;
import com.xjy.ftpmanagment.pojo.FtpUser;
import com.xjy.ftpmanagment.pojo.SysParam;
import com.xjy.ftpmanagment.pojo.SysUser;

public class FtpManagmentService extends BaseService{
	private FtpUserDao ftpUserDao;
	private SysUserDao sysUserDao;
	private SysParamDao sysParamDao;
	public FtpUserDao getFtpUserDao() {
		return ftpUserDao;
	}
	public void setFtpUserDao(FtpUserDao ftpUserDao) {
		this.ftpUserDao = ftpUserDao;
	}
	public SysUserDao getSysUserDao() {
		return sysUserDao;
	}
	public void setSysUserDao(SysUserDao sysUserDao) {
		this.sysUserDao = sysUserDao;
	}
	public SysParamDao getSysParamDao() {
		return sysParamDao;
	}
	public void setSysParamDao(SysParamDao sysParamDao) {
		this.sysParamDao = sysParamDao;
	}
	/**
	 * 保存其它设置中相关参数
	 * @param sysParams
	 */
	public void saveSysParams(List<SysParam> sysParams){
		if(sysParams!=null&&sysParams.size()>0){
			for(SysParam sysParam:sysParams){
				this.sysParamDao.update(sysParam);
			}
		}
	}
	/**
	 * 获取其它设置中相关参数
	 * @param condition
	 * @param parameters
	 * @return
	 */
	public List<SysParam> getSysParams(String condition,Object[]parameters){
		if(condition!=null && !condition.trim().equals("")){
			if(null!=parameters&& parameters.length>0){
				return this.sysParamDao.find(condition, parameters, null);
			}else{
				return this.sysParamDao.find(condition, null, null);
			}
		}
		return this.sysParamDao.find(null, null, null);
	}
	/**
	 * 获取默认收件人
	 * @param condition
	 * @param parameters
	 * @return
	 */
	public List<SysUser> getSysUsers(String condition,Object[]parameters){
		if(condition!=null && !condition.trim().equals("")){
			if(null!=parameters&& parameters.length>0){
				return this.sysUserDao.find(condition, parameters, null);
			}else{
				return this.sysUserDao.find(condition, null, null);
			}
		}
		return this.sysUserDao.find(null, null, null);
	}
	/**
	 * 批量删除收件人
	 * @param ids
	 */
	public void deleteSysUsers(long[] ids){
		for(long id:ids)
			this.sysUserDao.delById(id);
	}
	/**
	 * 保存收件人
	 * @param sysUser
	 */
	public void saveOrUpdateSysUser(SysUser sysUser){
		List<SysUser> sysUsers=this.sysUserDao.find("mail=?", new Object[]{sysUser.getMail().trim()}, null);
		if(sysUsers!=null && sysUsers.size()>0){
			SysUser sysExistUser=sysUsers.get(0);
			sysExistUser.setUsername(sysUser.getUsername());
			sysExistUser.setDescription(sysUser.getDescription());
			this.sysUserDao.update(sysExistUser);
			SysUser currSysUser=this.sysUserDao.get(sysUser.getId());
			if(currSysUser!=null){
				this.sysUserDao.delete(currSysUser);
			}
		}else{
			this.sysUserDao.saveOrUpdate(sysUser);
		}
	}
	/**
	 * 获取FTP帐号
	 * @param page
	 * @param pageSize
	 * @param condition
	 * @param parameters
	 * @return
	 */
	public PageBean getFtpUsers(int page,int pageSize,String condition,Object [] parameters,String sort){
		return this.ftpUserDao.getPageBean(condition, parameters, sort, page, pageSize);
	}
	public void deleteFtpUsers(int [] ids){
		for(int id:ids){
			FtpUser ftpUser=this.ftpUserDao.get(id);
			FileUtil.delFolder(ftpUser.getDirHome());
			this.ftpUserDao.delete(ftpUser);
		}
	}
	public int getCount(String condition,Object[] parameters){
		return this.ftpUserDao.findTotalCount(condition, parameters);
	}
	public boolean saveFtpUser(String username,String password,String usertype,String noticetype,String tempDate,String mailReceiver) throws ParseException{
		try{
			FtpUser ftpUser=new FtpUser();
			ftpUser.setFtpUserName(username);
			ftpUser.setFtpPassword(EncryptionUtil.getMD5("xj"+password));
			List<SysParam> sysParams=this.sysParamDao.findAll();
			int days=0;
			String normalPath=null,tempPath=null;
			for(SysParam sysParam:sysParams){
				if(sysParam.getId().equals(2l)){
					days=Integer.valueOf(sysParam.getV1());
				}else{
					if(sysParam.getId().equals(3l)){
						normalPath=sysParam.getV1();
					}else{
						if(sysParam.getId().equals(4l)){
							tempPath=sysParam.getV1();
						}
					}
				}
			}
			ftpUser.setAccessRule(getAccessRule(username, usertype, normalPath, tempPath));
			ftpUser.setExpiration(getExpiration(usertype, tempDate, days));
			ftpUser.setDirHome(usertype.equals("3")?tempPath+"\\"+username:normalPath+"\\"+username);
			this.ftpUserDao.save(ftpUser);
		}catch (Exception e) {
			return false;
		}
		return true;
	}
	public String getAccessRule(String pathname,String usertype,String normalPath,String tempPath){
		String accessRule="";
		if(usertype.equals("1") ||usertype.equals("2") ){
			FileUtil.createFile(normalPath+"\\"+pathname);
			accessRule=normalPath+"\\"+pathname+"|RLP";
		}else{
			if(usertype.equals("3")){
				FileUtil.createFile(tempPath+"\\"+pathname);
				accessRule=normalPath+"\\"+pathname+"|RWAMLCDP";
			}
		}
		return accessRule;
	}
	public Timestamp getExpiration(String usertype,String tempdate,int days) throws ParseException{
		Calendar calendar=Calendar.getInstance();
		if(usertype.equals("1")){
			calendar.add(Calendar.DAY_OF_YEAR, days);
		}else{
			if(usertype.equals("2")){
				calendar.setTime(DateUtil.YMD.parse("1980-1-1"));
			}else{
				if(usertype.equals("3")){
					calendar.setTime(DateUtil.YMD.parse(tempdate));
				}
			}
		}
		Timestamp timestamp=new Timestamp(calendar.getTimeInMillis());
		return timestamp;
	}
}
